package com.xckj.zuoliao.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;

import com.xckj.zuoliao.R;
import com.xckj.zuoliao.bean.LoginBean;
import com.xckj.zuoliao.databinding.ActivityLoginBinding;
import com.xckj.zuoliao.framework.BaseActivity;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.iview.ILoginView;
import com.xckj.zuoliao.presenter.LoginPresenter;
import com.xckj.zuoliao.utils.SpUtil;

import java.util.UUID;

/**
 * @Author: 代码的搬运工
 * @Describe: 登录
 * @Date: 2024/3/27 17:52
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements ILoginView {
    private String passWord;

    @Override
    protected int setStatusBarColor() {
        return R.color.login_color;
    }

    private LoginPresenter loginPresenter;

    @Override
    protected int loadLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        loginPresenter = new LoginPresenter(this);
    }

    @SuppressLint("HardwareIds")
    @Override
    protected void setListener() {
        dataBinding.etPassWord.setOnTextChangeListener((text, isComplete) -> {
            passWord = text;
        });
        dataBinding.btnLogin.setOnClickListener(view -> {
            if (TextUtils.isEmpty(passWord)) {
                showMsg("请输入密码");
                return;
            }
            if (TextUtils.isEmpty(SpUtil.getString(this, Constant.ANDROID_ID))) {
                SpUtil.put(this, Constant.ANDROID_ID, UUID.randomUUID().toString());
            }
            loginPresenter.login(passWord, SpUtil.getString(this, Constant.ANDROID_ID));
        });
    }

    @Override
    public void loginSuccess(LoginBean loginBean) {
        SpUtil.put(this, Constant.ID, loginBean.userinfo.id);
        SpUtil.put(this, Constant.ID_NAME, loginBean.userinfo.id_name);
        startActivity(new Intent(this, HomeActivity.class));
        finish();
    }

    @Override
    public void loginFailed(String message) {
        showMsg(message);
    }
}
